package org.btik.light.mobile.ui.devs.arm.model;

import androidx.lifecycle.ViewModel;
import org.btik.context.SimpleContext;
import org.btik.light.mobile.R;
import org.btik.light.mobile.meta.mo.DefaultDevMo;
import org.btik.light.mobile.meta.DefaultMeta;
import org.btik.light.mobile.meta.DevMeta;
import org.btik.light.mobile.meta.DevModel;

import java.util.function.BiConsumer;


public class ArmViewModel extends ViewModel implements DevModel {

    private DefaultMeta meta;

    private ArmMo mo;

    /**
     * SeekBar 刷新器
     */
    private BiConsumer<Integer, Integer> setPosShow;

    public void setSetPosShow(BiConsumer<Integer, Integer> setPosShow) {
        this.setPosShow = setPosShow;
    }

    public void setMo(DefaultDevMo mo) {
        this.mo = (ArmMo) mo;
    }

    /**
     * 加载舵机的位置
     */
    public int pos(int index, int defVal) {
        int pos = mo.getPosAt(index);
        if (pos == -1) {
            return defVal;
        }
        return pos;
    }

    public void setPos(int index, int val) {

        if (val != mo.getPosAt(index)) {
            setPosShow.accept(index, val);
        }
        mo.setPos(index, val);
    }

    public String getDev() {
        return mo.getSn();
    }


    @Override
    public DevMeta getMeta() {
        if (meta == null) {
            newMeta();
        }
        return meta;
    }

    private void newMeta() {
        String devType = mo.getDevType();
        DefaultMeta meta = SimpleContext.get(devType);
        this.meta = meta;
        meta.setDevTypeResStringId(R.string.ServoArm);
    }
}
